<?php
/**
 * Created by PhpStorm.
 * @author 清凉Tea <973110519@qq.com>
 * Date: 2018/3/9
 * Time: 12:31
 */

namespace app\common\library;

use think\Config;

class Sms
{
    // 单例对象
    protected static $instance;

    // phpmailer对象
    protected $sms;

    // 错误内容
    protected $_error = '';

    // 配置
    public $options = [];

    /**
     * 初始化
     * @access public
     * @param array $options 参数
     * @return Sms
     */
    public static function getInstance($options = [])
    {
        if (is_null(self::$instance))
        {
            self::$instance = new static($options);
        }
        return self::$instance;
    }

    /**
     * 构造函数
     * @param array $options
     */
    private function __construct($options = [])
    {
        if($smsClass = Config::get('site.sms_class')){
            $smsClass = "{$smsClass}";
            $this->sms = new $smsClass();
        }
        if ($config = Config::get('site.sms_config')){
            $this->options = array_merge($this->options, $config);
        }
    }

    /**
     * 获取最后产生的错误
     */
    public function getError()
    {
        return $this->_error;
    }

    protected function setError($error)
    {
        $this->_error = $error;
    }

    public function send($data)
    {
        if($this->sms == null || empty($this->options)){
            $this->setError(__('sms config is null'));
        }
        return $this->sms->send($this->options,$data);
    }
}